﻿using System.Collections.Generic;
using System.Linq;
using eCommerce.Core.Domain.Addresses;
using eCommerce.Core.Domain.Products;
using eCommerce.Core.Domain.Securitys;
using eCommerce.Core.Domain.ShoppingCarts;
using eCommerce.Core.Domain.Shops;

namespace eCommerce.Core.Domain.Customers
{
    public class CustomerMapper
    {
        public static IList<Customer> ToCustomers(IList<Customer> customers)
        {
            return customers.Select(ToCustomer).ToList();
        }

        public static Customer ToCustomer(Customer customer)
        {
            return new Customer()
            {
                Id = customer.Id,
                NickName = customer.NickName,
                Intro = customer.Intro,
                Active = customer.Active,
                ShippingAddress = ToShippingAddress(customer.ShippingAddress),
                Addresses = ToAddresss(customer.Addresses),
                CreatedOn = customer.CreatedOn,
                CustomerRoles = ToCustomerRoles(customer.CustomerRoles),
                Deleted = customer.Deleted,
                Email = customer.Email,
                Password = customer.Password,
                ShoppingCartItems = ToShoppingCartItems(customer.ShoppingCartItems),
                SelectedPaymentMethodSystemName = customer.SelectedPaymentMethodSystemName,
                Username = customer.Username,
                UserGuid = customer.UserGuid,
                Shop = ToShop(customer.Shop),
                ShopId = customer.ShopId,
                CustomerLogoId = customer.CustomerLogoId,
                CustomerLogo = customer.CustomerLogo !=null? new CustomerLogo()
                    {
                        Id = customer.CustomerLogo.Id,
                        PictureId = customer.CustomerLogo.PictureId
                   }:null
            };
        }


        private static Shop ToShop(Shop shop)
        {
            if (shop == null)
            {
                return null;
            }
            else
            {

                return new Shop()
                {
                    Id = shop.Id,
                    ShopName = shop.ShopName,
                };
            }
        }

        private static ICollection<ShoppingCartItem> ToShoppingCartItems(ICollection<ShoppingCartItem> shoppingCartItems)
        {
                return null;
            //if (shoppingCartItems == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    return shoppingCartItems.Select(c => new ShoppingCartItem()
            //    {
            //        Id = c.Id,
            //        CustomerId = c.CustomerId,
            //        Quantity = c.Quantity,
            //        ProductVariantId = c.ProductVariantId,
            //        AttributesXml = c.AttributesXml,
            //        ProductVariant = new ProductVariant()
            //        {
            //            Id = c.ProductVariant.Id,
            //            Name = c.ProductVariant.Name,
            //            Price = c.ProductVariant.Price,
            //            Published = c.ProductVariant.Published,
            //            StockQuantity = c.ProductVariant.StockQuantity,
            //            Product = new Product()
            //            {
            //                Id = c.ProductVariant.Product.Id,
            //                Published = c.ProductVariant.Product.Published,
            //                Shop = new Shop()
            //                {
            //                    Id = c.ProductVariant.Product.ShopId,
            //                    CustomerId = c.ProductVariant.Product.Shop.CustomerId
            //                }
            //            }

            //        }
            //    }).ToList();
            //}
        }


        private static ICollection<CustomerRole> ToCustomerRoles(ICollection<CustomerRole> customerRoles)
        {
            if (customerRoles == null)
            {
                return null;
            }
            else
            {
                return customerRoles.Select(c => new CustomerRole()
                {
                    Active = c.Active,
                    SystemName = c.SystemName,
                    PermissionRecords = c.PermissionRecords.Select(t => new PermissionRecord()
                    {
                        SystemName = t.SystemName,

                    }).ToList()
                }).ToList();
            }
        }


        private static ICollection<Address> ToAddresss(ICollection<Address> addresses)
        {
            if (addresses == null)
            {
                return null;
            }
            else
            {
                return addresses.Select(c => new Address()
                {
                    Id = c.Id,
                    Email = c.Email,
                    Province = c.Province,
                    CreatedOn = c.CreatedOn,
                    MobileNumber = c.MobileNumber,
                    PhoneNumber = c.PhoneNumber,
                    ZipPostalCode = c.ZipPostalCode,
                    Address2 = c.Address2,
                    Address1 = c.Address1,
                    City = c.City,
                    LastName = c.LastName,
                    FirstName = c.FirstName,

                }).ToList();
            }
        }

        private static Address ToShippingAddress(Address shippingAddress)
        {
            if (shippingAddress == null)
            {
                return null;
            }
            return new Address()
            {
                Address1 = shippingAddress.Address1,
                Address2 = shippingAddress.Address2,
                City = shippingAddress.City,
                CreatedOn = shippingAddress.CreatedOn,
                Email = shippingAddress.Email,
                FirstName = shippingAddress.FirstName,
                Id = shippingAddress.Id,
                LastName = shippingAddress.LastName,
                MobileNumber = shippingAddress.MobileNumber,
                PhoneNumber = shippingAddress.PhoneNumber,
                Province = shippingAddress.Province,
                ZipPostalCode = shippingAddress.ZipPostalCode,





            };
        }
    }
}